﻿@*
    配置
*@
@using NewLife;
@using System.ComponentModel;
@using System.Reflection;
@using NewLife.Reflection;
@using XCode;
@{
    var obj = (Object)Model;
    var fields = ViewBag.Fields as IList<DataField>;
    var groupFields = fields.GroupBy(e => e.Category + "").ToList();
}

<div class="row g-5 g-xl-5">
    <div class="col-xl-3">
        <div class="card card-xxl-stretch mb-xl-8">
            <div class="card-header">
                <h4 class="card-title text-muted">系统选项</h4>
            </div>

            <div class="card-body">
                @await Html.PartialAsync("_Object_Config_Nav", obj)
            </div>
        </div>
    </div>

    <div class="col-xl-9">
        <div class="card card-xxl-stretch mb-xl-8">
            @using (Html.BeginForm("Update", null))
            {
                <div class="card-header">
                    <div class="card-title">
                        @if (groupFields.Count > 1)
                        {
                            <ul class="nav">
                                @for (int i = 0; i < groupFields.Count; i++)
                                {
                                    var key = groupFields[i].Key;
                                    key = key.IsNullOrEmpty() ? "默认选项" : (key.Length < 4 ? key + "选项" : key);
                                    var cls = "nav-link btn btn-sm btn-color-muted btn-active-secondary fs-6 px-4 me-1 ";
                                    <li class="nav-item">
                                        <a class="@(i == 0 ? cls + "active" : cls)" data-bs-toggle="tab" href="@("#object_tab"+i)">@key</a>
                                    </li>
                                }
                            </ul>
                        }
                        else
                        {
                            <h4>&nbsp;</h4>
                        }
                    </div>
                    @if (this.Has(PermissionFlags.Update))
                    {
                        <div class="card-toolbar">
                            <button type="submit" class="btn btn-sm btn-outline btn-outline-primary align-self-center me-2"><i class="fas fa-save text-active-primary"></i><strong>保存</strong></button>
                            <button type="button" class="btn btn-sm btn-outline btn-outline-dashed btn-outline-default btn-active-light align-self-center" onclick="history.go(-1);"><i class="fas fa-reply"></i><strong>取消</strong></button>
                        </div>
                    }
                </div>

                <div class="card-body pt-0">
                    @Html.AntiForgeryToken()
                    @Html.ValidationSummary(true)

                    @if (groupFields.Count > 1)
                    {
                        <div class="tab-content">
                            @for (int i = 0; i < groupFields.Count; i++)
                            {
                                <div class="tab-pane fade @(i==0 ? "active show" : "")" id="@("object_tab"+i)">
                                    @foreach (var pi in groupFields[i])
                                    {
                                        var name = pi.Name;
                                        var val = obj.GetValue(name);
                                        var display = pi.DisplayName;
                                        var des = pi.Description;
                                        var dataSource = pi.DataSource;

                                        <div class="row d-flex align-items-center mt-5 mb-1 pt-1">
                                            <div class="col-lg-3 d-flex align-items-center justify-content-lg-end">
                                                <label class="fs-6 me-2" for="@name">@display</label>
                                            </div>

                                            <div class="col-lg-6">
                                                @if (dataSource != null)
                                                {
                                                    @await Html.PartialAsync("_Form_Type_DropDownList", new ListBoxModel(name, dataSource(obj), val))
                                                }
                                                else
                                                {
                                                    @await Html.PartialAsync("_Form_Editor_Item", new ItemModel(name, val, pi.Type))
                                                }
                                                <span class="fw-bolder fs-6 text-dark"></span>
                                                @if (!des.IsNullOrEmpty())
                                                {
                                                    <label class="text-muted fs-10">@des</label>
                                                }
                                            </div>

                                        </div>
                                    }
                                </div>
                            }
                        </div>
                    }
                    else
                    {
                        @foreach (var pi in fields)
                        {
                            var name = pi.Name;
                            var val = obj.GetValue(name);
                            var display = pi.DisplayName;
                            var des = pi.Description;
                            var dataSource = pi.DataSource;

                            <div class="row d-flex align-items-center mt-5 mb-1 pt-1">
                                <div class="col-lg-3 d-flex align-items-center justify-content-lg-end">
                                    <label class="fs-6 me-2" for="@name">@display</label>
                                </div>

                                <div class="col-lg-6">
                                    @if (dataSource != null)
                                    {
                                        @await Html.PartialAsync("_Form_Type_DropDownList", new ListBoxModel(name, dataSource(obj), val))
                                    }
                                    else
                                    {
                                        @await Html.PartialAsync("_Form_Editor_Item", new ItemModel(name, val, pi.Type))
                                    }
                                    <span class="fw-bolder fs-6 text-dark"></span>
                                    @if (!des.IsNullOrEmpty())
                                    {
                                        <label class="text-muted fs-10">@des</label>
                                    }
                                </div>

                            </div>
                        }
                    }
                </div>

                @if (this.Has(PermissionFlags.Update))
                {
                    <div class="card-footer d-flex justify-content-end py-6">
                        <button type="submit" class="btn btn-sm btn-outline btn-outline-primary align-self-center me-2"><i class="fas fa-save text-active-primary"></i><strong>保存</strong></button>
                        <button type="button" class="btn btn-sm btn-outline btn-outline-dashed btn-outline-default btn-active-light align-self-center" onclick="history.go(-1);"><i class="fas fa-reply"></i><strong>取消</strong></button>
                    </div>
                }
            }
        </div>
    </div>
</div>


